package com.stu.manager.studentscoremanagersys.Utils;

import com.alibaba.fastjson2.JSON;
import com.stu.manager.studentscoremanagersys.consts.constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
public class ResponseUtils {
    /**
     * 由于前端请求抖被拦截后，并没有经过controller，返回只能通过PrintWriter
     * 拦截校验的时候写入到前端浏览器的响应结果
     * @param response
     * @param obj
     */
    public static  void ResponseJson(HttpServletResponse response,Object obj){
        response.setCharacterEncoding(constants.UTF_8_NAME);
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        try {
            PrintWriter printWriter = response.getWriter();
            printWriter.print(JSON.toJSONString(obj));
            printWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.info("写入前端异常");
        }


    }
}
